package com.xique.park.controller.auto;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.xique.common.core.constant.Constants;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.poi.ExcelUtil;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.page.TableDataInfo;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.park.bean.domain.ParkDeviceNew;
import com.xique.park.bean.domain.ParkDeviceOnlineLog;
import com.xique.park.service.service.IParkDeviceNewService;
import com.xique.park.service.service.IParkDeviceOnlineLogService;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 设备上下线日志记录Controller
 *
 * @author xique
 * @date 2021-12-09
 */
@RestController
@RequestMapping("/deviceLog")
public class ParkDeviceOnlineLogController extends BaseController {

    @Autowired
    private IParkDeviceOnlineLogService parkDeviceOnlineLogService;

    @Autowired
    private IParkDeviceNewService parkDeviceNewService;

    /**
     * 查询设备上下线日志记录列表
     */
    @PreAuthorize(hasPermi = "park:log:list")
    @GetMapping("/list/V2")
    public TableDataInfo listV2(ParkDeviceOnlineLog parkDeviceOnlineLog) {
        startPage();
        List<ParkDeviceOnlineLog> list = parkDeviceOnlineLogService.selectParkDeviceOnlineLogList(parkDeviceOnlineLog);
        return getDataTable(list);
    }

    @PreAuthorize(hasPermi = "park:deviceLog:list")
    @GetMapping("/list")
    public TableDataInfo list(ParkDeviceOnlineLog parkDeviceOnlineLog) {
        Long parkId = SecurityUtils.getParkId();
        if (parkId == null) {
            return getDataTable(Lists.newArrayList());
        }
        String deviceCode = parkDeviceOnlineLog.getDeviceCode();
        if (StrUtil.isBlank(deviceCode)) {
            // 为空，说明查找所有设备的上下线日志
            ParkDeviceNew parkDeviceNew = new ParkDeviceNew();
            parkDeviceNew.setParkId(parkId);
            parkDeviceNew.setIsMain(Constants.YES);
            List<ParkDeviceNew> parkDeviceNewList = parkDeviceNewService.selectParkDeviceNewList(parkDeviceNew);
            if (CollUtil.isEmpty(parkDeviceNewList)) {
                return getDataTable(Lists.newArrayList());
            }
            List<String> deviceCodeList = parkDeviceNewList.stream().map(ParkDeviceNew::getCode).collect(Collectors.toList());
            parkDeviceOnlineLog.setDeviceCodeList(deviceCodeList);
        }
        startPage();
        parkDeviceOnlineLog.setParkId(parkId);
        return getDataTable(parkDeviceOnlineLogService.selectParkDeviceOnlineLogList(parkDeviceOnlineLog));
    }

    /**
     * 导出设备上下线日志记录列表
     */
    @PreAuthorize(hasPermi = "park:log:export")
    @Log(title = "设备上下线日志记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ParkDeviceOnlineLog parkDeviceOnlineLog) throws IOException {
        List<ParkDeviceOnlineLog> list = parkDeviceOnlineLogService.selectParkDeviceOnlineLogList(parkDeviceOnlineLog);
        ExcelUtil<ParkDeviceOnlineLog> util = new ExcelUtil<>(ParkDeviceOnlineLog.class);
        util.exportExcel(response, list, "设备上下线日志记录数据");
    }

    /**
     * 获取设备上下线日志记录详细信息
     */
    @PreAuthorize(hasPermi = "park:log:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(parkDeviceOnlineLogService.selectParkDeviceOnlineLogById(id));
    }

    /**
     * 新增设备上下线日志记录
     */
    @PreAuthorize(hasPermi = "park:log:add")
    @Log(title = "设备上下线日志记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ParkDeviceOnlineLog parkDeviceOnlineLog) {
        return toAjax(parkDeviceOnlineLogService.insertParkDeviceOnlineLog(parkDeviceOnlineLog));
    }

    /**
     * 修改设备上下线日志记录
     */
    @PreAuthorize(hasPermi = "park:log:edit")
    @Log(title = "设备上下线日志记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ParkDeviceOnlineLog parkDeviceOnlineLog) {
        return toAjax(parkDeviceOnlineLogService.updateParkDeviceOnlineLog(parkDeviceOnlineLog));
    }

    /**
     * 删除设备上下线日志记录
     */
    @PreAuthorize(hasPermi = "park:log:remove")
    @Log(title = "设备上下线日志记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(parkDeviceOnlineLogService.deleteParkDeviceOnlineLogByIds(ids));
    }
}
